/* eslint-disable unicorn/prefer-module */
import path from 'node:path';

import { defineConfig } from '@vben/vite-config';

import AutoImport from 'unplugin-auto-import/vite';
import ElementPlus from 'unplugin-element-plus/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import Inspector from 'vite-plugin-vue-inspector';

export default defineConfig(async () => {
  return {
    application: {},
    vite: {
      plugins: [
        ElementPlus({
          format: 'esm',
        }),
        Inspector({
          // toggleButtonPos: 'bottom-right',
          toggleButtonVisibility: 'always',
        }),
        AutoImport({
          dts: 'src/auto-import.d.ts', // 路径下自动生成文件夹存放全局指令
          imports: ['vue', 'vue-router'],
          resolvers: [ElementPlusResolver()],
        }),
        Components({
          // 自动导入 `./components` 下的组件
          dirs: ['src/components'],
          // 生成类型声明
          dts: 'src/components.d.ts',
          resolvers: [ElementPlusResolver()],
        }),
      ],
      resolve: {
        alias: {
          '#': path.resolve(__dirname, 'src'),
        },
      },
      // 兼容后端微服务
      server: {
        proxy: {
          // AI服务
          '/cs-api': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/cs-api/, ''),
            target: 'http://192.168.1.106:8886/cs-api',
            ws: true,
          },
          // 系统服务
          '/sys-api': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/sys-api/, ''),
            target: 'http://192.168.1.106:8888/sys-api',
            ws: true,
          },
        },
      },
    },
  };
});
